package com.ruoyi.crm.service.impl;

import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.Seq;
import com.ruoyi.crm.domain.*;
import com.ruoyi.crm.domain.vo.ContractVo;
import com.ruoyi.crm.mapper.ActivityMapper;
import com.ruoyi.crm.mapper.ContractMapper;
import com.ruoyi.crm.mapper.CourseMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.crm.mapper.BusinessMapper;
import com.ruoyi.crm.service.IBusinessService;

import javax.annotation.Resource;

/**
 * 商机管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-04-21
 */
@Service
public class BusinessServiceImpl implements IBusinessService 
{
    @Resource
    private BusinessMapper businessMapper;
    @Resource
    private ActivityMapper activityMapper;
    @Resource
    private ContractMapper contractMapper;
    @Resource
    private CourseMapper courseMapper;

    /**
     * 查询商机管理
     * 
     * @param id 商机管理主键
     * @return 商机管理
     */
    @Override
    public Business selectBusinessById(Long id)
    {
        return businessMapper.selectBusinessById(id);
    }

    /**
     * 查询商机管理列表
     * 
     * @param business 商机管理
     * @return 商机管理
     */
    @Override
    public List<Business> selectBusinessList(Business business)
    {
        return businessMapper.selectBusinessList(business);
    }

    /**
     * 新增商机管理
     * 
     * @param business 商机管理
     * @return 结果
     */
    @Override
    public int insertBusiness(Business business)
    {
        business.setCreateTime(DateUtils.getNowDate());
        return businessMapper.insertBusiness(business);
    }

    /**
     * 修改商机管理
     * 
     * @param business 商机管理
     * @return 结果
     */
    @Override
    public int updateBusiness(Business business)
    {
        return businessMapper.updateBusiness(business);
    }

    /**
     * 批量删除商机管理
     * 
     * @param ids 需要删除的商机管理主键
     * @return 结果
     */
    @Override
    public int deleteBusinessByIds(Long[] ids)
    {
        return businessMapper.deleteBusinessByIds(ids);
    }

    /**
     * 删除商机管理信息
     * 
     * @param id 商机管理主键
     * @return 结果
     */
    @Override
    public int deleteBusinessById(Long id)
    {
        return businessMapper.deleteBusinessById(id);
    }

    @Override
    public int giveBack(Long id) {
        Business business=businessMapper.selectBusinessById(id);
        business.setStatus(Clue.Status.WXS.getValue());
        return businessMapper.updateBusiness(business);
    }

    @Resource
    private AssignRecordServiceImpl assignRecordService;
    @Override
    public int reset(Long id) {
        AssignRecord assignRecord=new AssignRecord();
        assignRecord.setType(AssignRecord.Type.BUSINESS.name());
        assignRecord.setUpdateBy(SecurityUtils.getUsername());
        assignRecord.setUpdateTime(new Date());
        assignRecord.setAssignId(id);
        return assignRecordService.insertAssignRecord(assignRecord);
    }

    @Override
    public int transform(ContractVo contractVo) {
        Business business=businessMapper.selectBusinessById(contractVo.getId());
        if (business.getCourseId()==null)throw new RuntimeException("请选择课程");
        business.setStatus(Clue.Status.ZHT.getValue());
        business.setUpdateBy(SecurityUtils.getUsername());
        business.setUpdateTime(new Date());
        businessMapper.updateBusiness(business);

        Contract contract=new Contract();
        BeanUtils.copyProperties(business,contract);
        contract.setActivityId(contractVo.getId());
        contract.setContractNo(contractVo.getContractNo());
        contract.setPhone(contractVo.getPhone());
        contract.setFileName(contractVo.getFileName());
        contract.setId(Seq.getId());
        contract.setStatus("4");
        contractOrder(contract);
        return contractMapper.insertContract(contract);
    }

    private void contractOrder(Contract contract) {
        Course course=courseMapper.selectCourseById(contract.getCourseId());
        Activity activity=null;

        if (contract.getActivityId()!=null){
            activity=activityMapper.selectActivityById(contract.getActivityId());
        }
        if (activity==null){
            contract.setContractOrder(course.getPrice());
        }else {
            if ("1".equals(activity.getType())){
                contract.setDiscountType("课程折扣");
                contract.setContractOrder((long) (activity.getDiscount()/10.0*course.getPrice()));
            }else {
                contract.setDiscountType("优惠卷");
                contract.setContractOrder(course.getPrice()-activity.getVouchers());
            }
            contract.setContractOrder(course.getPrice());
        }
    }


}
